var loaderUtils = require('loader-utils');

module.exports = function (source) {
    const options = loaderUtils.getOptions(this)
    const res = replaceMatched(source, options)
    return res
};

function replaceMatched (source, options) {
  const data = source.replace(/#ENV\((\w+)\)/g, (match, $1) => {
    return process.env[$1] || ''
  });

  return data.replace(/\/\*\s*IFTRUE\((\w+)\)\s*\*\/([\s\S]+?)\/\*\s*ENDIF\s*\*\//g, (match, $1, $2) => {
    const isKeep = options[$1]
    return isKeep ? $2 : ''
  });
}
